package com.xiao.blecontrol;

import android.util.Log;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.Locale;

/**
 * Byte工具
 */

public class ByteUtils {

    private static final  String HEADER = "5354";
    private static final  String END = "0D0A";

    private static final Integer SEND_HEAD_1=83;
    private static final Integer SEND_HEAD_2=84;
    private static final Integer PASSWORD=20190905;

    private static final Integer SEND_END_1 =10;
    private static final Integer SEND_END_2 =13;

    /**
     * 十六进制字符串转byte[]
     *
     * @param hex 十六进制字符串
     * @return byte[]
     */
    public static byte[] hexStr2Byte(String hex) {
        if (hex == null) {
            return new byte[]{};
        }

        // 奇数位补0
        if (hex.length() % 2 != 0) {
            hex = "0" + hex;
        }

        int length = hex.length();
        ByteBuffer buffer = ByteBuffer.allocate(length / 2);
        for (int i = 0; i < length; i++) {
            String hexStr = hex.charAt(i) + "";
            i++;
            hexStr += hex.charAt(i);
            byte b = (byte) Integer.parseInt(hexStr, 16);
            buffer.put(b);
        }
        return buffer.array();
    }

    /**
     * byte[]转十六进制字符串
     *
     * @return 十六进制字符串
     */
    public static String byteArrayToHexString(byte[] array) {
        if (array == null) {
            return "";
        }
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < array.length; i++) {
            buffer.append(byteToHex(array[i]));
        }
        return buffer.toString();
    }

    /**
     * byte转十六进制字符
     *
     * @param b byte
     * @return 十六进制字符
     */
    public static String byteToHex(byte b) {
        String hex = Integer.toHexString(b & 0xFF);
        if (hex.length() == 1) {
            hex = '0' + hex;
        }
        return hex.toUpperCase(Locale.getDefault());
    }



    /**
     * 加密
     * @param controlStr
     * @param str
     * @return
     */
    public static String encryp(String controlStr, String str){
        String encrpStr = HEADER+controlStr+str+END;
        return encrpStr;
    }

    /**
     * 解密
     * @param controlStr
     * @param passwordData
     * @return
     */
    public static String decryp(String controlStr,String passwordData){
        String header = HEADER+controlStr;
        Integer length = passwordData.length();
        String hexStr = passwordData.substring(header.length(),length-END.length());
        return Integer.valueOf(hexStr,16).toString();
    }
    public static String getControlStr(String data){
       return data.substring(HEADER.length(),HEADER.length()+2);
    }

    public static String getValue(String str){
        char [] strArray = str.toCharArray();
        StringBuffer newStr = new StringBuffer();
        for(int i=14 ;i<18;i++){
            newStr.append(strArray[i]);
        }
        return newStr.toString();
    }

    public static String getModelValue(String str){
        char [] strArray = str.toCharArray();
        StringBuffer newStr = new StringBuffer();
        for(int i=18 ;i<20;i++){
            newStr.append(strArray[i]);
        }
        return newStr.toString();
    }

    public static String getTimeValue(String str){
        char [] strArray = str.toCharArray();
        StringBuffer newStr = new StringBuffer();
        for(int i=20 ;i<22;i++){
            newStr.append(strArray[i]);
        }
        return newStr.toString();
    }

    public static byte[] packageSendData(Integer control, Integer data1,Integer data2,Integer data3,Integer data4){
        try {
            byte[] headerByte = hexStr2Byte(HEADER);
            byte[] endByte = hexStr2Byte(END);
            byte[] controlByte = hexStr2Byte(Integer.toHexString(control));
            byte[] passwordByte = hexStr2Byte(Integer.toHexString(PASSWORD));
            byte[] dataByte1 = hexStr2Byte(Integer.toHexString(data1));
            byte[] dataByte2 = hexStr2Byte(Integer.toHexString(data2));
            byte[] dataByte3=  hexStr2Byte(Integer.toHexString(data3));
            byte[] dataByte4= hexStr2Byte(Integer.toHexString(data4));
            return byteMergerAll(headerByte, controlByte, passwordByte, dataByte1,dataByte2,dataByte3,dataByte4,endByte);
        }catch (Exception e){
            return null;
        }

    }
    private static byte[] byteMergerAll(byte[]... values) {
        int length_byte = 0;
        for (int i = 0; i < values.length; i++) {
            length_byte += values[i].length;
        }
        byte[] all_byte = new byte[length_byte];
        int countLength = 0;
        for (int i = 0; i < values.length; i++) {
            byte[] b = values[i];
            System.arraycopy(b, 0, all_byte, countLength, b.length);
            countLength += b.length;
        }
        return all_byte;
    }
    public static String getInputValue(String str){
        char [] strArray = str.toCharArray();
        StringBuffer newStrH = new StringBuffer();
        StringBuffer newStrL = new StringBuffer();
        for(int i=14 ;i<18;i++){
            newStrH.append(strArray[i]);
        }
        return newStrH.toString();
    }

   
    public static String getOutValue(String str){
        char [] strArray = str.toCharArray();
        StringBuffer newStrH = new StringBuffer();
        for(int i=18 ;i<22;i++){
            newStrH.append(strArray[i]);
        }

        return newStrH.toString();
    }
    public static String getOutValueL(String str){
        char [] strArray = str.toCharArray();
        StringBuffer newStrL = new StringBuffer();
        for(int i=20 ;i<22;i++){
            newStrL.append(strArray[i]);
        }
        return newStrL.toString();
    }

    public static String getContorlValueStr(String str){
        char [] strArray = str.toCharArray();
        StringBuffer newStr = new StringBuffer();
        for(int i = 14;i<16;i++){
            newStr.append(strArray[i]);
        }
        return newStr.toString();
    }
    public static String getParamVale(String type,String str){
        char [] strArray = str.toCharArray();
        StringBuffer newStr = new StringBuffer();
        switch (type){
            case "output_ratio" :
                for(int i=16;i<18;i++){
                    newStr.append(strArray[i]);
                }
                break;
            case "time" :
                for(int i=18;i<20;i++){
                    newStr.append(strArray[i]);
                }
                break;
            case "rising_error" :
                for(int i=20;i<22;i++){
                    newStr.append(strArray[i]);
                }
                break;
            case "descent_error" :
                for(int i=22;i<24;i++){
                    newStr.append(strArray[i]);
                }
                break;
            case "sample_time" :
                for(int i=24;i<26;i++){
                    newStr.append(strArray[i]);
                }
                break;
            case "close" :
                for(int i=26;i<28;i++){
                    newStr.append(strArray[i]);
                }
                break;
            default:
                break;

        }
        return newStr.toString();
    }

    public static byte[] packageSendDataNew(String control, Integer data1,Integer data2,Integer data3,Integer data4,Integer data5,Integer data6){
        try {
            byte[] headerByte = hexStr2Byte(HEADER);
            byte[] endByte = hexStr2Byte(END);
            byte[] controlByte = hexStr2Byte(control);
            byte[] passwordByte = hexStr2Byte("013416B9");
            byte[] dataByte1 = hexStr2Byte(Integer.toHexString(data1));
            byte[] dataByte2 = hexStr2Byte(Integer.toHexString(data2));
            byte[] dataByte3=  hexStr2Byte(Integer.toHexString(data3));
            byte[] dataByte4= hexStr2Byte(Integer.toHexString(data4));
            byte[] dataByte5= hexStr2Byte(Integer.toHexString(data5));
            byte[] dataByte6= hexStr2Byte(Integer.toHexString(data6));
            return byteMergerAll(headerByte, controlByte, passwordByte, dataByte1,dataByte2,dataByte3,dataByte4,dataByte5,dataByte6,endByte);
        }catch (Exception e){
            Log.i("ByteUtil组装数据错误:",e.getMessage());
            return null;
        }

    }
    public static boolean isNormalDate(String str){
        boolean flag = true;
        String start = str.substring(0,4);
        String end = str.substring(str.length()-4);
        if(! start.equals(HEADER) || !end.equals(END)){
            flag = false;
        }
        return  flag;
    }
}
